//
//  EditChooseTypeView.m
//  WeCommunity
//
//  Created by mac on 2017/8/10.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "EditChooseTypeView.h"

@interface EditChooseTypeView ()

@property (nonatomic,strong) UIView * contentView;

@property (nonatomic,strong) NSArray * contentTitleArray;

@property (nonatomic,strong) NSMutableArray * contentBtnArray;

@property (nonatomic,assign) NSInteger index;

@end

@implementation EditChooseTypeView

-(UIView *)contentView
{
    if(!_contentView){
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 130)];
        _contentView.backgroundColor = WHITE_COLOR;
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 100 , 40)];
        titleLabel.textColor = [UIColor colorWithHex:0xaeaeae alpha:1.0];
        titleLabel.text = KNSLocal(@"选择分类");
        titleLabel.font = PFR_FONT(12);
        [_contentView addSubview:titleLabel];
    }
    return _contentView;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.clipsToBounds = YES;
        
        self.backgroundColor  = [UIColor colorWithWhite:0.0 alpha:0.1];
        
        [self addSubview:self.contentView];
        
        [self setChooseViewAnimationWithShow:NO animation:NO];
        
        //点击手势识别器
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processGesture:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


#pragma mark - 手势
- (void)processGesture:(UISwipeGestureRecognizer *)gesture
{
    if ([self.delegate respondsToSelector:@selector(editChooseViewSelectedTitle:selectedIndex:)]) {
        [self.delegate editChooseViewSelectedTitle:self.contentTitleArray[self.index] selectedIndex:self.index];
    }
}

-(void)setContentTitles:(NSArray *)contentTitles selectedIndex:(NSInteger)index
{
    self.index = index;
    self.contentTitleArray = contentTitles;

    self.contentBtnArray = [NSMutableArray array];
    CGFloat ButtonWidth = (CGRectGetWidth(self.bounds) - 12 * 5) / 4;
    CGFloat ButtonHeight = 32;
    
    for (int i = 0; i < self.contentTitleArray.count; i++) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(12 + (ButtonWidth + 12) * (i % 4) ,40 + (ButtonHeight + 12) * (i / 4), ButtonWidth, ButtonHeight)];
        [btn setTitle:self.contentTitleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:TINT_GREEN_COLOR forState:UIControlStateSelected];
        btn.titleLabel.font = PFR_FONT(14);
        btn.backgroundColor = GRAY_COLOR;
        btn.layer.cornerRadius = 4;
        btn.clipsToBounds = YES;
        btn.exclusiveTouch = YES;
        btn.tag = 10 + i;
        [btn addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        if (index == i) {
            btn.selected = YES;
        }
        [self.contentView addSubview:btn];
        
        [self.contentBtnArray addObject:btn];
    }
}

-(void)processBtn:(UIButton *)sender
{
    if (sender.selected) {
        return;
    }
    NSInteger index = sender.tag - 10;
    
    for (int i = 0; i < self.contentBtnArray.count; i++) {
        UIButton * btn = self.contentBtnArray[i];
        btn.selected = index == i;
    }
    
    if ([self.delegate respondsToSelector:@selector(editChooseViewSelectedTitle:selectedIndex:)]) {
        self.index = sender.tag - 10;
        [self.delegate editChooseViewSelectedTitle:self.contentTitleArray[index] selectedIndex:index];
    }
}

-(void)setChooseViewAnimationWithShow:(BOOL)show animation:(BOOL)animation
{
    if (show) {
        self.hidden = NO;
    }
    
    NSTimeInterval animationTime = 0;
    if (animation) {
        animationTime = 0.2;
    }
    
    [UIView animateWithDuration:animationTime animations:^{
        
        CGFloat animationValue = 0;
        if (!show) {
            animationValue = -CGRectGetHeight(self.contentView.bounds);
        }
        self.contentView.transform = CGAffineTransformMakeTranslation(0,animationValue);
        
    } completion:^(BOOL finished) {
        if (!show) {
            self.hidden = YES;
        }
    }];
    
}


@end
